﻿using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core.Entities;
using Glorysoft.EAP.Core.Enums;
using Glorysoft.EAP.EAPClient.Model;
using Glorysoft.EAP.EAPClient.ViewModel;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Glorysoft.EAP.EAPClient
{
    /// <summary>
    /// LogWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NeedleMarkDetectionSelectWafer : Window
    {

        List<CheckBox> headerChecks = new List<CheckBox>();
        int chbNum = 0;
        private void CheckBox_Loaded_1(object sender, RoutedEventArgs e)
        {
            CheckBox cbtemp = (CheckBox)sender;
            headerChecks.Add(cbtemp);
            EnumVisual(this);
            chbNum = headerChecks.Count;
        }
        public Model.RunService ClientInfo
        {
            get
            {
                return Model.RunService.Current;
            }
        }
        private readonly Logger ServiceLogger = XfwContext.EapLogger;
        public NeedleMarkDetectionSelectWafer()
        {
            InitializeComponent();
            this.DataContext = new SelectWaferViewModel();
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.Topmost = true;
            Rect rc = SystemParameters.WorkArea;//获取工作区大小 
            this.Left = (rc.Width) * 4 / 6;//设置位置 
            this.Top = (rc.Height) / 3;
            ClientInfo.EQPInfo.SelectWaferID = "";
          
        }
        /// <summary>
        /// 选择全选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e)
        {

            foreach (var item in headerChecks)
            {
                item.IsChecked = true;


            }
            ClientInfo.EQPInfo.SelectWaferID = "";
            foreach (SLotMap ovj in this.ListGrid.ItemsSource)
            {
                ovj.isSelect = true;
                ClientInfo.EQPInfo.SelectWaferID += ovj.SlotId + ";";
            }

        }

        /// <summary>
        /// 取消全选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e)
        {
            var selChe = headerChecks.Where(x => x.IsChecked == false);
            if (selChe != null && selChe.Count() == chbNum - 1)
            {
                headerChecks[0].IsChecked = false;
                return;
            }
            foreach (var item in headerChecks)
            {

                item.IsChecked = false;
            }
            foreach (SLotMap ovj in this.ListGrid.ItemsSource)
            {
                ovj.isSelect = false;
            }
            ClientInfo.EQPInfo.SelectWaferID = "";
            //for (int i = 0; i < this.ListGrid.Items.Count; i++)
            //{
            //    var cntr = ListGrid.ItemContainerGenerator.ContainerFromIndex(i);
            //    DataGridRow ObjROw = (DataGridRow)cntr;
            //    if (ObjROw != null)
            //    {
            //        FrameworkElement objElement = ListGrid.Columns[0].GetCellContent(ObjROw);
            //        if (objElement != null)
            //        {
            //            //if (objElement.GetType().ToString().EndsWith("cRUID"))
            //            //{
            //            System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
            //            if (objChk.IsChecked == true)
            //            {
            //                objChk.IsChecked = false;
            //            }
            //            //}
            //        }
            //    }
            //}
        }

        private void ButtonClear_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            ClientInfo.EQPInfo.SelectWaferID = "";
            var messages = TranslateMsg.GetTranslation("SelectWaferIDCancel");
            if (!(messages == null || messages.Count == 0))
            {
                ClientInfo.EQPInfo.LotList[0].ShowMsg(MessageOrigin.EAP, MessageType.Info, messages[RunService.Current.ClientLanguage]);
                XfwContext.DbLogger.Information("EAPClient Tip:" + messages[RunService.Current.ClientLanguage]);
            }
        }

        public void EnumVisual(Visual myVisual)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
            {
                Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
                if (childVisual.GetType().Name == "CheckBox")
                {
                    CheckBox cb = childVisual as CheckBox;
                    if (cb != null)
                    {
                        headerChecks.Add(cb);
                    }
                }
                EnumVisual(childVisual);
            }
        }

        /// <summary>
        /// 选中，取消列表checked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            var sm = cb.DataContext as SLotMap;
            if (cb != null)
            {
                sm.isSelect = cb.IsChecked;
                if (cb.IsChecked == true)
                {
                    ClientInfo.EQPInfo.SelectWaferID += sm.SlotId + ";";
                    if (ClientInfo.EQPInfo.SelectWaferID.Length > 2)
                    {
                        SelectWaferOrderBy(ClientInfo.EQPInfo.SelectWaferID);
                    }

                }
                else
                {
                    if (!string.IsNullOrEmpty(ClientInfo.EQPInfo.SelectWaferID))
                    {
                        //var index = ClientInfo.EQPInfo.SelectWaferID.IndexOf(sm.SlotId);
                        var waferIDList = ClientInfo.EQPInfo.SelectWaferID.Split(';').Where(t => t.Length > 0).ToList();
                        waferIDList.Remove(sm.SlotId);
                        ClientInfo.EQPInfo.SelectWaferID = string.Join(";", waferIDList.ToArray()) + ";";
                        //ClientInfo.EQPInfo.SelectWaferID = ClientInfo.EQPInfo.SelectWaferID.Replace(sm.SlotId + ";", "");

                    }
                }
                var selChe = headerChecks.Where(x => x.IsChecked == true);
                if (selChe != null && selChe.Count() == chbNum - 1)
                {
                    headerChecks[0].IsChecked = true;
                    return;
                }

                selChe = headerChecks.Where(x => x.IsChecked == false);
                if (selChe != null && selChe.Count() == chbNum - 1)
                {
                    headerChecks[0].IsChecked = false;

                    return;
                }

            }

        }

        private void ListGrid_OnMouseUp(object sender, MouseButtonEventArgs e)
        {

        }


        public void SelectWaferOrderBy(string selectWafers)
        {
            ClientInfo.EQPInfo.SelectWaferID = "";
            var selectWaferList = selectWafers.Split(';').Where(t => !string.IsNullOrEmpty(t.ToString())).OrderBy(t => int.Parse(t.ToString())).ToArray();
            ClientInfo.EQPInfo.SelectWaferID = string.Join(";", selectWaferList) + ";";
            //foreach (var item in selectWaferList)
            //{
            //    ClientInfo.EQPInfo.SelectWaferID += item+";";
            //}

        }

        //private void DataGridLoading(object sender, DataGridRowEventArgs e)
        //{
        //    dynamic item = e.Row.Item;
        //    if (item!=null)
        //    {
        //        if (item.State.ToString().equals("aaa"))
        //        {
        //            e.Row.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff7575"));
        //        }
        //    }
         

        //}
        
    }



}
